package conn

import (
	"context"
	"gitee.com/injoyai/goutil/oss/io"
	"net"
)

type (
	Conn    = io.Client
	Message = io.Message
)

// New 新建连接
// Close()会直接关闭连接,不管数据是否传输完成
// SetDeadline(time.Time{})会等数据传输完成再关闭
func New(c net.Conn) *Conn {
	return NewWithContext(context.Background(), c)
}

func NewWithContext(ctx context.Context, c net.Conn) *Conn {
	cli := io.NewClientWithContext(ctx, c)
	cli.SetKey(c.RemoteAddr().String())
	cli.SetRedialFunc(func() (io.ReadWriteCloser, error) {
		return net.Dial(c.LocalAddr().Network(), c.RemoteAddr().String())
	})
	return cli
}
